package net.lab1024.sa.admin.module.business.erp.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.dao.*;
import net.lab1024.sa.admin.module.business.erp.domain.entity.*;
import net.lab1024.sa.admin.module.business.erp.domain.form.add.WageAddForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.query.WageQueryForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.update.WageUpdateForm;
import net.lab1024.sa.admin.module.business.erp.domain.vo.WageVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 工资 Service
 *
 * @Author AKITA
 * @Date 2025-04-19 13:22:18
 * @Copyright AKITA
 */

@Service
public class WageService {

    @Resource
    private WageDao wageDao;
    @Autowired
    private WageaDao wageaDao;
    @Autowired
    private AccountbDao accountbDao;
    @Autowired
    private AccountcDao accountcDao;
    @Autowired
    private AccountDao accountDao;

    /**
     * 分页查询
     */
    public PageResult<WageVO> queryPage(WageQueryForm queryForm)  {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<WageVO> list = wageDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<Long> add(WageAddForm addForm) {
        WageEntity wageEntity = SmartBeanUtil.copy(addForm, WageEntity.class);
        wageDao.insert(wageEntity);
        return ResponseDTO.ok(wageEntity.getDocEntry());
    }

    /**
     * 更新
     *
     */
    @Transactional
    public ResponseDTO<String> update(WageUpdateForm updateForm) {
        WageEntity wageEntity = SmartBeanUtil.copy(updateForm, WageEntity.class);

        if (wageDao.updateById(wageEntity) == 0) {
            throw new RuntimeException("数据已被更新，刷新后重试！");
        }
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {

        wageDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    @Transactional
    public ResponseDTO<String> delete(Long docEntry) {
        WageEntity wageEntity = wageDao.selectById(docEntry);
        List<AccountEntity> accountEntities = accountDao.selectList(new LambdaQueryWrapper<AccountEntity>().eq(AccountEntity::getAbsId, wageEntity.getAbsId()));
        if (accountEntities != null) {
            for (AccountEntity accountEntity : accountEntities) {
                accountEntity.setDocStatus("O");
                accountDao.updateById(accountEntity);
            }
        }
        wageDao.deleteById(docEntry);
        wageaDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<WageEntity> info(Long docEntry) {
        WageEntity wageEntity = wageDao.selectById(docEntry);
        wageEntity.setLista(wageaDao.selectList(new LambdaQueryWrapper<WageaEntity>().eq(true, WageaEntity::getDocEntry, docEntry)));
        return ResponseDTO.ok(wageEntity);
    }


    @Transactional
    public ResponseDTO<String> excute(WageUpdateForm updateForm) {

        List<WageaEntity> entityList = wageDao.countCost(updateForm.getAbsId());
        int lineNum = 0;
        WageaEntity wageaEntity1 = new WageaEntity();
        for (WageaEntity wageaEntity : entityList) {
            lineNum++;
            wageaEntity1.setDocEntry(updateForm.getDocEntry());
            wageaEntity1.setLineNum(lineNum);
            wageaEntity1.setUserId(wageaEntity.getUserId());
            wageaEntity1.setUserName(wageaEntity.getUserName());
            wageaEntity1.setBasicWage(wageaEntity.getBasicWage());
            wageaEntity1.setPostWage(wageaEntity.getPostWage());
            wageaEntity1.setMeritsWage(wageaEntity.getMeritsWage());
            wageaEntity1.setOverTimeWage(wageaEntity.getOverTimeWage());
            wageaEntity1.setDeduWage(wageaEntity.getDeduWage());
            BigDecimal add = wageaEntity.getBasicWage().add(wageaEntity.getPostWage()).add(wageaEntity.getMeritsWage()).add(wageaEntity.getDanPostWage()).
                    add(wageaEntity.getMealWage()).add(wageaEntity.getOverTimeWage()).add(wageaEntity.getPhoneWage()).add(wageaEntity.getAgeWage()).
                    add(wageaEntity.getReissueWage()).add(wageaEntity.getOtherWage()).add(wageaEntity.getOther());
            wageaEntity1.setPayableWage(add);
            BigDecimal add1 = wageaEntity.getPensionSafe().add(wageaEntity.getHealthSafe()).add(wageaEntity.getLoseSafe()).add(wageaEntity.getTaxableAmount()).
                    add(wageaEntity.getPersIncoTax()).add(wageaEntity.getRebateTax()).add(wageaEntity.getDeduWage());
            BigDecimal subtract = add.subtract(add1);
            wageaEntity1.setPayableAmount(subtract);
            wageaEntity1.setBaseEntry(wageaEntity.getDocEntry());
            wageaEntity1.setType(wageaEntity.getType());
            wageaDao.insert(wageaEntity1);

            List<AccountEntity> accountEntities = accountDao.selectList(new LambdaQueryWrapper<AccountEntity>().eq(AccountEntity::getAbsId, updateForm.getAbsId()));
            if (accountEntities != null) {
                for (AccountEntity accountEntity : accountEntities) {
                    accountEntity.setDocStatus("C");
                    accountDao.updateById(accountEntity);
                }

            }

        }
        return ResponseDTO.ok();
    }


    public ResponseDTO<String> shareService(Long docEntry) {
        List<AccountbEntity> entityList = wageDao.shareDao(docEntry);
        for (AccountbEntity accountbEntity : entityList) {
            accountbDao.update(accountbEntity, new LambdaQueryWrapper<AccountbEntity>().eq(true, AccountbEntity::getDocEntry, accountbEntity.getDocEntry()).eq(true, AccountbEntity::getLineNum, accountbEntity.getLineNum()));
        }
        List<AccountcEntity> entityLista = wageDao.shareaDao(docEntry);
        for (AccountcEntity accountcEntity : entityLista) {
            accountcDao.update(accountcEntity, new LambdaQueryWrapper<AccountcEntity>().eq(true, AccountcEntity::getDocEntry, accountcEntity.getDocEntry()).eq(true, AccountcEntity::getLineNum, accountcEntity.getLineNum()));

        }
        return ResponseDTO.ok();

    }
}
